layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: '../Member/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print',
        //  {
        //     title: '提示',
        //     layEvent: 'LAYTABLE_TIPS',
        //     icon: 'layui-icon-tips'
        // }
        ],
        page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
            //,curr: 5 //设定初始在第 5 页
            ,groups: 5 //只显示 1 个连续页码
            ,first: 1 //不显示首页
            ,last: 'count'/'limit' //不显示尾页
        },
        cols: [[
            {type: "checkbox", width: 50},
            // {field: 'id', width: 80, title: 'ID', sort: true},
            {field: 'username', width: 100, title: '客户名称'},
            {field: 'userphone', width: 120, title: '电话', sort: true},
            {field: 'detailaddress',  title: '详细地址'}
            ,
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        // page: true,
        skin: 'line'
        ,contentType: 'application/json'
        ,method:'Post'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        // console.log("Debug search:",data);

        //执行搜索重载
        table.reload('currentTableId', {
            page:{
                curr:1
            },
            where : {
                searchParams: data.field
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add'){// 监听添加操作
            var index = layer.open({
                title: '添加用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'Members-add.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'import') {  // 监听导入操作
            var index = layer.open({
                title: '导入用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'Members-import.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.confirm("确认删除？？",function(index){
                $.ajax({
                    url:"/Member/del",
                    type:"post",
                    data:JSON.stringify(data),
                    datatype:"json",
                });
                table.reload('currentTableId');
                layer.close(index);
            });
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        // console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        // console.log("Debug mem data:",data);
        if (obj.event === 'edit') {

            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'Members-edit.html?id=' + data.mem_uid,
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });

});